﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime;
using System.IO;
using System.Web.Security;
public partial class admin_RecoveryPassword : SitePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void PasswordRecovery1_UserLookupError(object sender, EventArgs e)
    {
     
    }
    protected void PasswordRecovery1_VerifyingUser(object sender, LoginCancelEventArgs e)
    {
      
    }
    protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
    {
     //   Literal ltrFailureText = (Literal)PasswordRecovery1.FindControl("FailureText");
        e.Cancel = true;
        MembershipUser user = Membership.GetUser(PasswordRecovery1.UserName);
        string nessPass = user.ResetPassword();
        SendMail(user.UserName, nessPass, user.Email);
    }
    private void SendMail(string userName,string passs,string email)
    {
        string path = Server.MapPath("~/" + clsConfigData.DEFAULT_MAIL_TEMPLATE_RECOVERY_PASS);
        string[] template = ReadFile(path);
        clsEmail mail = new clsEmail();
        //clsConfigData.DEFAULT_MAIL_SERVER,clsConfigData.DEFAULT_MAIL_FROM,clsConfigData.DEFAULT_MAIL_TIMEOUT
        mail.Sender = clsConfigData.DEFAULT_MAIL_FROM;
        mail.Receiver = email;
        mail.Body = string.Format(template[1],userName, passs,this.ApplicationRoot);
        mail.Subject = template[0];
        mail.BodyFormat = enmBodyFormat.HTML;
        mail.TimeOut = clsConfigData.DEFAULT_MAIL_TIMEOUT;        
        if (mail.SendExchangeMail())
            PasswordRecovery1.SuccessText = Resources.Resource.MSG_RECOVERY_PASS_SUCSESSFULL;
        else
            PasswordRecovery1.SuccessText = Resources.Resource.MSG_MAIL_ERROR;
    }
    public string[] ReadFile(string path)
    {
        string title = string.Empty;
        string body = string.Empty;
        string[] lines = File.ReadAllLines(path);
        for (int i = 0; i < lines.Length; i++)
        {
            if (i == 0)
                title = lines[i].ToString();
            else
            {
                body += lines[i].ToString() + "\r\n";
            }
        }
        string[] result = new string[2];
        result[0] = title;
        result[1] = body;
        return result;
    }
}